package com.haikesi.api.subscribertable.controller;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import com.haikesi.api.subscribertable.domain.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.haikesi.api.base.ApiIndex;
import com.haikesi.api.base.Error;
import com.haikesi.api.base.R;
import com.haikesi.api.base.Type;
import com.haikesi.api.base.annotation.Token;
import com.haikesi.api.subscribertable.service.ISubscriberTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**  
 * @ClassName:  SubscriberTableController 
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: zqy     
 * @date:   2019年5月15日 下午5:25:53
 * @version V1.0
 * @Copyright: 2019 重庆海克斯科技有限公司. 
 * 注意：本内容仅限于重庆海克斯科技有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
@Api(description = "订阅账号相关接口", tags = ApiIndex.SubscriberTableController)
@RequestMapping(value = "/api/subscribertable")
@RestController
public class SubscriberTableController {

	@Autowired
	private ISubscriberTableService subscriberTableService;
	
	
	@ApiOperation(value = "查询列表")
    @GetMapping(value = "/list")
//    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
//    @Token
    public R<PageInfo<List<SubscriberTableVo>>> list(@ApiParam(value = "查询参数") @ModelAttribute SubscriberTableSearchVo subscriberTableSearchVo) {
        List<SubscriberTableVo> list = subscriberTableService.getList(subscriberTableSearchVo);
        PageInfo pageInfo = new PageInfo(list);
        return new R(pageInfo);
    }
	
	@ApiOperation(value = "创建订阅账号")
    @PostMapping(value = "")
//  @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
//  @Token
    public R create(
            @ApiParam(value = "添加参数") @Valid @RequestBody SubscriberTableSaveVo subscriberTableSaveVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        subscriberTableService.create(subscriberTableSaveVo);
        return new R();
    }


    @ApiOperation(value = "修改订阅账号")
    @PutMapping(value = "")
//    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
//    @Token
    public R update(
            @ApiParam(value = "修改参数") @Valid @RequestBody SubscriberTableUpdateVo subscriberTableUpdateVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        subscriberTableService.update(subscriberTableUpdateVo);
        return new R();
    }


    @ApiOperation(value = "删除订阅账号")
    @DeleteMapping(value = "")
//    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
//    @Token
    public R delete(
            @ApiParam(value = "删除参数")@RequestParam(value = "id", required = false) String id) {
    	subscriberTableService.delete(id);
        return new R();
    }
    

    @ApiOperation(value = "订阅")
    @GetMapping(value = "/subscriber")
//    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
//    @Token
    public R subscriber(
            @ApiParam(value = "订阅账号") @RequestParam(value = "subscriberNumber",required = false) String subscriberNumber) {
    	subscriberTableService.subscriber(subscriberNumber);
        return new R();
    }

    @ApiOperation(value = "绑定设备")
    @PostMapping(value = "/bindDevice")
//    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
//    @Token
    public R bindDevice(
            @ApiParam(value = "修改参数") @Valid @RequestBody SubscriberBindDeviceVo bindDeviceVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        subscriberTableService.bindDevice(bindDeviceVo);
        return new R();
    }
    @ApiOperation(value = "解绑绑定设备")
    @PostMapping(value = "/unbindDevice")
//    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
//    @Token
    public R unbindDevice(
            @ApiParam(value = "修改参数") @Valid @RequestBody SubscriberBindDeviceVo bindDeviceVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        subscriberTableService.unbindDevice(bindDeviceVo);
        return new R();
    }
	
	
	
	
	
	
}
